<template>
  <div :class="wrapclass">
    <!-- 淘内小程序 没有file文件流，只展示 url  -->
    <img
      v-if="url"
      class="cell-img"
      :src="validSrc"
      imageFit="cover"
      @click="onClick"
    />
    <div
      v-if="status === 'pending'"
      class="image-uploader-cell-mask"
    >
      <span class="image-uploader-cell-loading">
        <span class="image-uploader-cell-mask-message">上传中...</span>
      </span>
    </div>
    <!-- 上传中时，不需要删除按钮, 当上传失败时，使用上传失败的一套删除 -->
    <div
      v-if="status !== 'pending' && status === 'fail'"
      class="image-uploader-cell-fail-delete"
    >
      <div
        class="image-uploader-cell-fail-delete-icon-wrap"
        @click="onDelete"
      >
        <img
          class="img-fail-delete"
          :src="deleteup"
        />
      </div>
      <div class="image-uploader-cell-fail-delete-text">上传失败</div>
    </div>
    <!-- 上传中时，不需要删除按钮 -->
    <img
      v-else-if="status !== 'pending'"
      class="image-uploader-cell-delete"
      :src="deleteup"
      @click="onDelete"
    />
  </div>
</template>

<script>
export default {
  name: "ImageUploader",
  components: {},
  model: {
    prop: "value",
    event: "change",
  },
  props: {
    status: {
      type: String,
      default: "",
    },
    url: {
      type: String,
      default: "",
    },
    value: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    const deleteup = '//www.xnbz.site/static/file/deleteup.png'
    return {
      loading: false,
      deleteup,
    }
  },
  computed: {
    // 因为淘宝小程序使用套件上传图片，因此没有dataUrl，此变量暂时没有用
    validSrc() {
      if (this.url) {
        return this.url
      }
      if (this.file) {
        return URL.createObjectURL(this.file)
      }
      return ""
    },
    wrapclass() {
      return `image-uploader-cell ${this.status === "fail" && "image-uploader-cell-fail"}`
    },
  },
  watch: {
    validSrc() {
      // return URL.revokeObjectURL(src)
    },
  },
  methods: {
    onClick() {
      this.$emit("click")
    },
    onDelete() {
      this.$emit("delete")
    },
  },
}
</script>

<style lang="less">
.image-uploader-cell {
  flex-shrink: 0;
  display: inline-block;
  height: 107px;
  width: 107px;
  border-radius: 8px;
  position: relative;
  margin-right: 11px;
  .cell-img {
    display: block;
    width: 100%;
    height: 100%;
    border-radius: 8px;
  }
  .image-uploader-cell-delete {
    height: 20px;
    width: 20px;
    position: absolute;
    right: 0px;
    top: 0px;
  }
  .image-uploader-cell-mask {
    position: absolute;
    top: 0px;
    right: 0px;
    left: 0px;
    bottom: 0px;
    background-color: rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    border-radius: 8px;
  }

  .image-uploader-cell-fail-delete {
    position: absolute;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    background: rgba(0, 0, 0, 0.2);
  }
  .img-fail-delete {
    display: block;
    height: 20px;
    width: 20px;
    border-radius: 10px;
  }
  .image-uploader-cell-fail-delete-icon-wrap {
    text-align: center;
    margin-bottom: 2px;
  }
  .image-uploader-cell-fail-delete {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
  }
  .image-uploader-cell-fail-delete-text {
    color: white;
  }
}
</style>
